package com.tpu.osu.rating.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tpu.osu.rating.Vote;
import com.tpu.osu.rating.Pin;

public class VoteDAORAM implements VoteDAO
{

    private Map<String, Vote> votes;
    private Map<String, Pin> pins;

    public VoteDAORAM()
    {
        votes = new HashMap<String, Vote>();
    }

    public void delete(String aPIN) throws Exception
    {
        votes.remove(aPIN);
    }

    public List<Vote> enumerate() throws Exception
    {
        return new ArrayList<Vote>(votes.values());
    }

    public void insert(Vote aVote) throws Exception
    {
        votes.put(aVote.getPin(), aVote);
    }

    public List<Vote> selectByLector(String aLector) throws Exception
    {

        List<Vote> result = new ArrayList<Vote>();

        for (Vote v : votes.values())
        {
            if (v.getLector().equals(aLector))
            {
                result.add(v);
            }
        }

        return result;
    }

    public Vote selectByPIN(String aPIN) throws Exception
    {
        return votes.get(aPIN);
    }

    public List<Vote> selectByGroup(String aGroup) throws Exception
    {
    	
    	List<Vote> result = new ArrayList<Vote>();
    	
    	for (Vote v : votes.values())
    	{
    		for (Pin p : pins.values())
    			{
    				if (v.getPin().equalsIgnoreCase(p.getId()))
    				{
    					if(p.getGroup().equals(aGroup))
    					{
    						result.add(v);	
    					}
    				}
    			
    			}
    	}
    	return result;
    }

    public List<Vote> selectByLectorAndGroup(String aLector, String aGroup) throws Exception
    {
    	
    	List<Vote> result = new ArrayList<Vote>();

        for (Vote v : votes.values())
        {
            if (v.getLector().equals(aLector))
            {
            	for (Pin p : pins.values())
    			{
    				if (v.getPin().equalsIgnoreCase(p.getId()))
    				{
    					if(p.getGroup().equals(aGroup))
    					{
    						result.add(v);	
    					}
    				}
    			
    			}
            }
        }
        return result;
    }

    public void update(Vote aVote) throws Exception
    {
    	for (Vote v : votes.values())
        {
    		if(v.getPin().equalsIgnoreCase(aVote.getPin()))
    			v.setComments(aVote.getComments());
        }

    }

}
